package com.eshbase.util;

import com.alibaba.fastjson.JSONObject;
import com.elastic.util.Esutil;
import com.eshbase.model.EsHbaseModel;
import com.google.gson.JsonObject;
import com.hbase.util.HbaseAdmin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @description: es-hbase操作类
 * @author: zyj
 * @create: 2019-12-26 09:54
 **/
@Service
public class EsHbaseUtil {

    @Autowired
    private HbaseAdmin hbaseAdmin;
    @Autowired
    private Esutil esutil;

    /**
    * @Description: 创建eshbase表名，es索引
    * @Param: [esHbaseModel]
    * @return: boolean true:创建成功
    * @Author: zyj
    * @Date: 2019/12/26
    */
    public boolean createTable(EsHbaseModel esHbaseModel){
        /**hbase表名，es索引名称*/
        String tableName = esHbaseModel.getTableName();
        String columnFamilyNmae = esHbaseModel.getColumnFamilyNmae();

        /**step1: 创建hbase表名*/
        int hb = hbaseAdmin.createOneTable(tableName, columnFamilyNmae);

        /**step2: 创建索引*/
        boolean b = esutil.createIndex(esHbaseModel.getEsHbaseIndexBeans(), tableName);

        if(hb!=0&&b){
            return true;
        }

        return false;
    }

    /**
    * @Description:
    * @Param: [jsonObject 内容, row 行号, columnFamilyNmae 列族, tableName 表名]
    * @return: boolean
    * @Author: zyj
    * @Date: 2019/12/26
    */
    public boolean insertContentOne(JSONObject jsonObject,JSONObject esJsonObject, String row, String columnFamilyNmae, String tableName){
        /**step1: 添加hbase数据*/
        boolean hb = hbaseAdmin.insertManyColumnRecords(tableName, row, jsonObject, columnFamilyNmae);

        /**step2: 添加es数据*/
        boolean es = esutil.inseartData(esJsonObject.toJSONString(), tableName);

        if(hb&&es){
            return true;
        }

        return false;
    }
}
